<?php
/* Copyright [2014] UW-Parkside
 * Authors: Matthew Zygowicz, Armando Narvaez
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

/**
 *This model controls the logic involved with manipulating Realms
 *and the Realms table.  Any operation involving aRealms should
 *be done in this class.
 */
class Realms extends CI_Model {

    /**
     *Load the database from the config file, and register this function
     *as a valid codeigniter model
     */
    public function __construct() {
        $this->load->database();
        parent::__construct();
    }

    /**
     *gets all available realms from the realms table greater than and equal to the
     *given realm
     *
     *@param string $realm the minimum access level to be retrieved
     *@return array returns an array of the realms
     */
    public function getAllRealms($realm) {
        //this function will return an array of all possible routes
        $sql = "SELECT vcRealm FROM `realms` WHERE iRid >= (Select iRid from `realms` where vcRealm=?)";
        $query = $this->db->query($sql, array($realm));
        return $query->result_array();
    }

    /**
     *Gets a realm id given the realms name string
     *
     *@param string $realm the name of the realm
     *@return int returns the iRid of a given realm
     */
   public function getRealmId($realm){
       $realm = strtoupper($realm);
       $sql = "SELECT iRid FROM `realms` WHERE UPPER(vcRealm)=?";
       $query = $this->db->query($sql, array($realm));
       $resultArray = $query->result_array();
       return $resultArray[0]['iRid'];
   }

}

?>